package com.example.xmldemo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    Button btn_read;
    TextView data;
    List<Person> personList=null;
    InputStream inputStream;

    //
    private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private static String[] PERMISSIONS_STORAGE = {
            "android.permission.READ_EXTERNAL_STORAGE",
            "android.permission.WRITE_EXTERNAL_STORAGE" };


    public static void verifyStoragePermissions(Activity activity) {

        try {
            //检测是否有写的权限
            int permission = ActivityCompat.checkSelfPermission(activity,
                    "android.permission.WRITE_EXTERNAL_STORAGE");
            if (permission != PackageManager.PERMISSION_GRANTED) {
                // 没有写的权限，去申请写的权限，会弹出对话框
                ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,REQUEST_EXTERNAL_STORAGE);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn_read = findViewById(R.id.btn_read);
        btn_read.setOnClickListener(this);
        data = findViewById(R.id.data);
        personList = new ArrayList<>();
        //
        verifyStoragePermissions(this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_read:
                String result="";
                inputStream = getResources().openRawResource(R.raw.itcase);
                if(inputStream==null){
                    Toast.makeText(this,"InputStream is null",Toast.LENGTH_SHORT).show();
                }else{
                    personList = XMLParsingMethods.readXmlByPull(inputStream);// 调用Pull
                   // personList = XMLParsingMethods.readXmlBySAX(inputStream);// 调用SAX
                   //  personList = XMLParsingMethods.readXmlByDOM(inputStream);// 调用DOM

                }
                if(personList!=null){
                    for(int i=0;i<personList.size();i++){
                        String message = "id = " + personList.get(i).getId() + " , name = " + personList.get(i).getName()
                                + " , age = " + personList.get(i).getAge()
                                + ".\n";
                        result += message;
                    }
                }else{
                    Toast.makeText(this,"persons is null",Toast.LENGTH_SHORT).show();
                }
                data.setText(result);
                XMLParsingMethods.createXmlFile(personList);


        }

    }
}